package {

import flash.display.Bitmap;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;

import mx.core.UIComponent;

public class EditLine extends UIComponent {
    [Embed(source="Line.png")]
    private const LINE_PNG:Class;

    private var _isDown:Boolean = false;
    public function get isDown():Boolean{
        return _isDown;
    }

    public var rect:Rectangle = new Rectangle(0,0,0,0);

    private var _mm:Main;
    /**
     * 构造函数
     * @param h_or_v 水平还是垂直
     */
    public function EditLine(h_or_v:Boolean, mm:Main) {
        _mm = mm;
        var bmp:Bitmap = new LINE_PNG() as Bitmap;
        bmp.height=2048;
        bmp.x = -bmp.width/2;
        bmp.y = -bmp.height/2;
        this.addChild(bmp);
        if (h_or_v) {
            this.rotation=90;
        }
        this.mouseEnabled = true;
        this.addEventListener(Event.ADDED_TO_STAGE, addToStage);
    }

    private function addToStage(e:Event):void{
        removeEventListener(Event.ADDED_TO_STAGE, addToStage);
        this.addEventListener(MouseEvent.MOUSE_DOWN, onMouse);
        stage.addEventListener(MouseEvent.MOUSE_UP, onMouse);
    }
    private function onMouse(e:MouseEvent):void{
        if(e.type==MouseEvent.MOUSE_DOWN)
        {
            e.stopPropagation();
            e.stopImmediatePropagation();
            _isDown = true;
            this.startDrag(false,rect);
        }
        else if(e.type==MouseEvent.MOUSE_UP)
        {
            _isDown = false;
            _mm.refresh(true);
            this.stopDrag();
        }
    }
}
}